package cn.krund.lyc.hotlerobot.utils;

import android.os.Environment;

import org.apache.log4j.Level;

import java.io.File;

import de.mindpipe.android.logging.log4j.LogConfigurator;

/**
 * @包名: cn.krund.lyc.hotlerobot.utils
 * @作者: 刘育成
 * @时间: 2018/3/22  10:11
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: ConfigureLog4j
 */


public class ConfigureLog4j {
    private static final int MAX_FILE_SIZE = 1024 * 1024 * 5;
    private static final String DEFAULT_LOG_FILE_NAME = "ServiceRobot.log";

    private static final String TAG = "ConfigureLog4j";

    public static void configure(String fileName) {
        final LogConfigurator logConfigurator = new LogConfigurator();
        try {
            if (isSdcardMounted()) {
                logConfigurator.setFileName(Environment.getExternalStorageDirectory()
                        + File.separator + "krund" + File.separator + "locallog"
                        + File.separator + fileName);
            } else {
                logConfigurator.setFileName("/data/data/krund.cn.krund.manage/files"
                        + File.separator + fileName);
            }
            logConfigurator.setMaxBackupSize(3);
            logConfigurator.setRootLevel(Level.DEBUG);
            logConfigurator.setFilePattern("%d\t%p/%c:\t%m%n");
            logConfigurator.setMaxFileSize(MAX_FILE_SIZE);
            logConfigurator.setImmediateFlush(true);
            logConfigurator.configure();
            android.util.Log.i(TAG, "Log4j config finish");
        } catch (Throwable throwable) {
            logConfigurator.setResetConfiguration(true);
            android.util.Log.e(TAG, "Log4j config error, use default config. Error:" + throwable);
        }
    }

    public static void configure() {
        configure(DEFAULT_LOG_FILE_NAME);
    }

    private static boolean isSdcardMounted() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }
}
